<?php

namespace app\controllers;

use app\models\Oauthtoken;
use app\models\User;
use app\models\UserPassport;
use app\models\uuid;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\Cookie;

class SiteController extends Controller
{
   
	public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => [ 'logout', 'signup'],
                'rules' => [

                ],
            ],
        ];
    }
       

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],

        ];
    }
    public $enableCsrfValidation = false;
    public function actionIndex()
    {

        return $this->render('index');
    }

    public function actionLogin()
    {
        $cookies= Yii::$app->response->cookies;
        $cookie=Yii::$app->request->cookies;
        if(!isset($cookie['curl_token'])){
            $token=new Oauthtoken();
            $token=$token->createAccessToken('123','444');
            $cookies->add(new Cookie(array(
                'name' => 'curl_token',
                'value' => $token['access_token'].'/==1',
                'expire'=>time()+86400
            )));
        }

        if(Yii::$app->request->isPost){
            $data=Yii::$app->request->post();
            if($cookie['curl_token']==$data['curl_token']){
                echo 'ok1';
            }
            var_dump($data);die();
        }
        return $this->render('login',['token'=>$cookie->getValue('curl_token')]);
    }

    public function actionLogout()
    {
        return $this->goHome();
    }
    public function actionRegister()
    {
//        $a= Yii::$app->getSecurity()->generatePasswordHash('980167048');//加密
//        $a= Yii::$app->getSecurity()->validatePassword('980167048', $a);//解密
        $error=false;
        if(Yii::$app->request->isPost){
            $dataPost=Yii::$app->request->post();

            if($dataPost['password']==$dataPost['password2']){

                $uuidClass=new uuid();
                $userPassportModel= new UserPassport();
                if($userPassportModel->findOne(['username'=>Yii::$app->request->post('username')])){
                    $error='帐号已存在';
                }else{
                    $id=$uuidClass->generateuuid();
                    $userPassportModel->uid=$id;
                    $userPassportModel->username=Yii::$app->request->post('username');
                    $userPassportModel->password=Yii::$app->getSecurity()->generatePasswordHash(Yii::$app->request->post('password'));;
                    $userPassportModel->log_time=time();
                    $userPassportModel->create_time=time();

                    $userModel=new User();
                    $userModel->id=$uuidClass->generateuuid();
                    $userModel->user_passport_id=$id;
                    $userModel->email=Yii::$app->request->post('email');
                    if($userPassportModel->save() && $userModel->save())
                    {
                        return $this->redirect('login',['uid'=>$userPassportModel->uid,'token'=>'33']);
                    }else{
                        $error= $userPassportModel->errors;
                    }
                }
                //var_dump(Yii::$app->request->post());die();
            }else{
                $error='两次密码不一致';
            }
        }
        return $this->render('register',['ip'=>Yii::$app->request->hostInfo,'error'=>$error]);
    }
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    public function actionAbout()
    {
        return $this->render('about');
    }
}
